﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Get : DataMapper User Guide</title>

<style type='text/css' media='all'>@import url('../css/userguide.css');</style>
<link rel='stylesheet' type='text/css' media='all' href='../css/userguide.css' />

<meta http-equiv='expires' content='-1' />
<meta http-equiv= 'pragma' content='no-cache' />
<meta name='robots' content='all' />

</head>

<body>

<!-- START NAVIGATION -->
<div id="nav"><div id="nav_inner"></div></div>
<div id="nav2"><a name="top">&nbsp;</a><a id="nav_toggle" href="#"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td><h1>DataMapper</h1></td>
<td id="breadcrumb_right"><a href="toc.html">Table of Contents Page</a></td>
</tr>
</table>
</div>
<!-- END NAVIGATION -->

<!-- START BREADCRUMB -->
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td id="breadcrumb">
<a href="http://stensi.com/">DataMapper Home</a> &nbsp;&#8250;&nbsp;
<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
Get
</td>
</tr>

</table>
<!-- END BREADCRUMB -->

<br clear="all" />


<!-- START CONTENT -->
<div id="content">


<h1>Get</h1>

<p>You'll find Get is used in a very similar way to retrieving data with CodeIgniter's <a href="http://codeigniter.com/user_guide/database/active_record.html">Active Record</a> class.  All the relevant query clauses from Active Record are available in DataMapper so you have the full power of retrieving data, in Active Record style!</p>

<p class="important"><strong>Note:</strong> There are enough differences between CodeIgniter and DataMapper's Active Record like query clauses that you should read on to be able to take full advantage of it.</p>

<br />

<p>Now, let's look at all the available methods.  We'll assume we have a DataMapper model setup, named Object.</p>

<h2>$object->get();</h2>

<p>Runs the selection query and returns the result.  Can be used by itself to retrieve all records from a table:</p>

<code>$o = new Object();<br />
$o->get();<br />
<br />
// The $o object is populated with all objects from its corresponding table</code>

<p>The first and second parameters enable you do set a limit and offset clause:</p>

<code>$o = new Object();<br />
$o->get(10, 20);<br />
<br />
// The $o object is populated with 10 objects from its corresponding table, starting from record 20</code>


<p>You can view the results in a couple of ways. Viewing the first result:</p>

<code>$o = new Object();<br />
$o->get();<br />
<br />
echo $o->title;
</code>

<p>Viewing all results:</p>

<code>$o = new Object();<br />
$o->get();<br />
<br />
foreach ($o->all as $obj)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;echo $obj->title;<br />
}</code>

<a name="validate-get"></a>
<h2>$object->validate->get();</h2>
<p>Normally, get() will generate its query from building up any query clauses you have setup before calling get().  If none are setup, it will default to selecting all records from the objects corresponding table.  However, there is a special situation where get() will use the values present within the current object.  This happens if you run the validate() function before a get() call.</p>
<p class="important"><strong>Note:</strong> When doing $object->validate()->get(); all other query clauses (such as select, where etc) will be ignored.</p>

<p>Ok, so to explain this, I'll use an example I've used before from the <a href="gettingstarted.html">Getting Started</a> page.  Taking part of the example from there, we see that the User model is setup to encrypt the password field with the salt from the matching users stored record (by username), when they attempt to login.</p>

<h3>User model (excerpt)</h3>
<code>
    function login()<br />
    {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;// Create a temporary user object<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$u = new User();<br />
<br />
    &nbsp;&nbsp;&nbsp;&nbsp;// Get this users stored record via their username<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$u->where('username', $this->username)->get();<br />
<br />
        &nbsp;&nbsp;&nbsp;&nbsp;// Give this user their stored salt<br />
        &nbsp;&nbsp;&nbsp;&nbsp;$this->salt = $u->salt;<br />
<br />
        &nbsp;&nbsp;&nbsp;&nbsp;// Validate and get this user by their property values,<br />
        &nbsp;&nbsp;&nbsp;&nbsp;// this will see the 'encrypt' validation run, encrypting the password with the salt<br />
        &nbsp;&nbsp;&nbsp;&nbsp;<strong>$this->validate()->get();</strong><br />
<br />
        &nbsp;&nbsp;&nbsp;&nbsp;// If the username and encrypted password matched a record in the database,<br />
        &nbsp;&nbsp;&nbsp;&nbsp;// this user object would be fully populated, complete with their ID.<br />
<br />
        &nbsp;&nbsp;&nbsp;&nbsp;// If there was no matching record, this user would be completely cleared so their id would be empty.<br />
        &nbsp;&nbsp;&nbsp;&nbsp;if (empty($this->id))<br />
        &nbsp;&nbsp;&nbsp;&nbsp;{<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Login failed, so set a custom error message<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->error_message('login', 'Username or password invalid');<br />
<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return FALSE;<br />
        &nbsp;&nbsp;&nbsp;&nbsp;}<br />
        &nbsp;&nbsp;&nbsp;&nbsp;else<br />
        &nbsp;&nbsp;&nbsp;&nbsp;{<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Login succeeded<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return TRUE;<br />
        &nbsp;&nbsp;&nbsp;&nbsp;}<br />
    }<br />
</code>

<p>Here's how the models login function was called. You can see the username and unencrypted password is set on the user object before calling the login function.</p>
<h3>Controller (excerpt)</h3>
<code>
// Create user object<br />
        $u = new User();<br />
<br />
        // Put user supplied data into user object<br />
        // (no need to validate the post variables in the controller,<br />
        // if you've set your DataMapper models up with validation rules)<br />
        $u->username = $this->input->post('username');<br />
        $u->password = $this->input->post('password');<br />
<br />
        // Attempt to log user in with the data they supplied, using the login function setup in the User model<br />
        // You might want to have a quick look at that login function up the top of this page to see how it authenticates the user<br />
        if ($u->login())<br />
        {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;echo '&lt;p&gt;Welcome ' . $this->username . '!&lt;/p&gt;';<br />
            &nbsp;&nbsp;&nbsp;&nbsp;echo '&lt;p&gt;You have successfully logged in so now we know that your email is ' . $this->email . '.&lt;/p&gt;';<br />
        }<br />
        else<br />
        {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;// Show the custom login error message<br />
            &nbsp;&nbsp;&nbsp;&nbsp;echo '&lt;p&gt;' . $this->error->login . '&lt;/p&gt;';<br />
        }<br />
</code>

<p>So, inside, the models login function, $object->validate->get(); is called which runs the validation functions, defined in the model, on the objects properties, and then it does a get using the validated properties.</p>


<h2>$object->get_where();</h2>

<p>Identical to the above function except that it permits you to add a "where" clause in the first parameter,
instead of using the $object->where() function:</p>

<code>$o = new Object();<br />
$o->get_where(array('id', => $id), $limit, $offset);</code>

<p>Please read the where function below for more information.</p>

<h2>$object->select();</h2>

<p>Permits you to write the SELECT portion of your query:</p>
<code>$o = new Object();<br />
$o->select('title, description');<br />
<br />
$o->get();<br />
<br />
// The $o object is populated with all objects from its corresponding table, but with only the title and description fields populated</code>

<p class="important"><strong>Note:</strong> If you are selecting all (*) from a table you do not need to use this function.  When omitted, DataMapper assumes you wish to SELECT *</p>

<h2>$object->select_max();</h2>
<p>Writes a "SELECT MAX(field)" portion for your query. You can optionally include a second parameter to rename the resulting field.</p>
<code>$o = new Object();<br />
$o->select_max('age');<br />
$o->get();<br />
<br />
// The $o object is populated with a single object from its corresponding table, but with only the age field populated, which contains the maximum age</code>


<h2>$object->select_min();</h2>
<p>Writes a "SELECT MIN(field)" portion for your query. As with <dfn>select_max()</dfn>, You can optionally include a second parameter to rename the resulting field.</p>
<code>$o = new Object();<br />
$o->select_min('age');<br />
$o->get();<br />
<br />
// The $o object is populated with a signle object from its corresponding table, but with only the age field populated, which contains the minimum age</code>

<h2>$object->select_avg();</h2>
<p>Writes a "SELECT AVG(field)" portion for your query. As with <dfn>select_max()</dfn>, You can optionally include a second parameter to rename the resulting field.</p>

<code>$o = new Object();<br />
$o->select_avg('age');<br />
$o->get();<br />
<br />
// The $o object is populated with a single object from its corresponding table, but with only the age field populated, which contains the average age</code>

<h2>$object->select_sum();</h2>
<p>Writes a "SELECT SUM(field)" portion for your query. As with <dfn>select_max()</dfn>, You can optionally include a second parameter to rename the resulting field.</p>
<code>$o = new Object();<br />
$o->select_sum('age');<br />
$o->get();<br />
<br />
// The $o object is populated with a single object from its corresponding table, but with only the age field populated, which contains the sum of all ages</code>







<h2>$object->where();</h2>
<p>This function enables you to set <strong>WHERE</strong> clauses using one of four methods:</p>

<p class="important"><strong>Note:</strong> All values passed to this function are escaped automatically, producing safer queries.</p>

<ol>
	<li><strong>Simple key/value method:</strong>

	<code>$o = new Object();<br />
	$o->where('name', $name);<br />
	<br />// When $o->get() is called, the above where clause will be included in the get query</code>
	
	<p>If you use multiple where function calls they will be chained together with <var>AND</var> between them:</p>

	
	<code>$o = new Object();<br />
	$o->where('name', $name);<br />
	$o->where('title', $title);<br />
	$o->where('status', $status);<br />
	<br />// When $o->get() is called, all of the above where clause will be included in the get query</code>

    </li>
	
	<li><strong>Custom key/value method:</strong>
	
	<p>You can include an operator in the first parameter in order to control the comparison:</p>
	
	<code>$o = new Object();<br />
	$o->where('name !=', $name);<br />
	$o->where('id <', $id);<br />
	<br />// When $o->get() is called, all of the above where clause will be included in the get query (with operators)</code>	</li>
	<li><strong>Associative array method:</strong>

	<code>
	$o = new Object();<br />
	$array = array('name' => $name, 'title' => $title, 'status' => $status);<br /><br />
	
	$o->where($array);
	<br /><br />// When $o->get() is called, the array of where clauses will be included in the get query</code>

	<p>You can include your own operators using this method as well:</p>

	<code>
	$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);<br /><br />

	$o = new Object();<br />
	$o>where($array);</code>	</li>
		<li><strong>Custom string:</strong>
		
		<p>You can write your own clauses manually:</p>

		<code>
		$where = "name='Joe' AND status='boss' OR status='active'";<br /><br />
		$o = new Object();<br />
		$o->where($where);</code></li>

	</ol>



<h2>$object->or_where();</h2>

<p>This function is identical to the one above, except that multiple instances are joined by OR:</p>

<code>$o = new Object();<br />
$o->where('name !=', $name);<br />
$o->or_where('id >', $id);
<br />
<br />// When $o->get() is called, all of the above where clause will be included in the get query separated by OR's</code>


<h2>$object->where_in();</h2>

<p>Generates a WHERE field IN ('item', 'item') SQL query joined with  AND if appropriate</p>
<code>$o = new Object();<br />
    $names = array('Frank', 'Todd', 'James');<br />
    $o->where_in('username', $names);<br /><br />
    // When $o->get() is called, all records where the username is Frank, Todd, or James will be returned</code>

<h2>$object->or_where_in();</h2>
<p>Generates a WHERE field IN ('item', 'item') SQL query joined with OR if appropriate</p>

<p><code>$o = new Object();<br />
    $firstnames = array('Frank', 'Todd', 'James');<br />
    $lastnames = array('Smith', 'Jones');<br />
    $o->where_in('firstname', $firstnames);<br />
    $o->or_where_in('lastname', $lastnames);<br />
    // When $o->get() is called, all records where the firstname is Frank, Todd, or James, or all records where the lastname is Smith or Jones, will be returned</code></p>
    
<h2>$o->where_not_in();</h2>
<p>Generates a WHERE field NOT IN ('item', 'item') SQL query joined with AND if appropriate</p>
<p><code>$o = new Object();<br />
    $names = array('Frank', 'Todd', 'James');<br />

    $o->where_not_in('username', $names);<br />
    // When $o->get() is called, all records where the username is not Frank, Todd, or James will be returned</code></p>

<h2>$object->or_where_not_in();</h2>
<p>Generates a WHERE field NOT IN ('item', 'item') SQL query joined with OR if appropriate</p>
<p><code>$o = new Object();<br />
    $firstnames = array('Frank', 'Todd', 'James');<br />
    $lastnames = array('Smith', 'Jones');<br />
    $o->where_not_in('firstname', $firstnames);<br />
    $o->or_where_not_in('lastname', $lastnames);<br />

    // When $o->get() is called, all records where the firstname is not Frank, Todd, or James, or all records where the lastname is not Smith or Jones, will be returned</code></p>

<h2>$object->like();</h2>
<p>This function enables you to generate <strong>LIKE</strong> clauses, useful for doing searches.</p>

<p class="important"><strong>Note:</strong> All values passed to this function are escaped automatically.</p>

<ol>
	<li><strong>Simple key/value method:</strong>

	<code>$o = new Object();<br />
	$o->like('title', 'match');
	<br /><br />// When $o->get() is called, all records with a title like match will be returned</code>
		
	<p>If you use multiple function calls they will be chained together with <var>AND</var> between them:</p>
	
	<code>$o = new Object();<br />
	$o->like('title', 'match');
	$o->like('body', 'match');
	<br /><br />// When $o->get() is called, all records with a title like match and a body like match will be returned</code>
	If you want to control where the wildcard (%) is placed, you can use an optional third argument. Your options are 'before', 'after' and 'both' (which is the default). 
	<code>$o = new Object();<br />
	$o->like('title', 'match', 'after');<br />
	<br />// When $o->get() is called, all records with a title starting with match will be returned</code>	</li>
	
	<li><strong>Associative array method:</strong>

	<code>

	$array = array('title' => $match, 'page1' => $match, 'page2' => $match);<br /><br />
	$o = new Object();<br />
	$o->like($array);
	<br /><br />// When $o->get() is called, all records with the title, page1, and page2 like the specified matches will be returned</code></li>
    </ol>
	
	
<h2>$object->or_like();</h2>
<p>This function is identical to the one above, except that multiple instances are joined by OR:</p>

<code>$o = new Object();<br />
$o->like('title', 'match');<br />

$o->or_like('body', $match);
<br />
<br />// When $o->get() is called, all records with a title like match or a body like match will be returned</code>



<h2>$object->not_like();</h2>
<p>This function is identical to <strong>like()</strong>, except that it generates NOT LIKE statements:</p>

<code>$o = new Object();<br />
	$o->not_like('title', 'match');
	<br /><br />// When $o->get() is called, all records with a title not like match will be returned</code>
	
	
<h2>$object->or_not_like();</h2>
<p>This function is identical to <strong>not_like()</strong>, except that multiple instances are joined by OR:</p>
<code>$o = new Object();<br />
	$o->like('title', 'match');
	$o->or_not_like('body', 'match');
	<br /><br />// When $o->get() is called, all records with a title like match or a body not like match will be returned</code>
	
	
<h2>$object->group_by();</h2>
<p>Permits you to write the GROUP BY portion of your query:</p>
	
<code>$o = new Object();<br />
$o->group_by('title');
<br /><br />// When $o->get() is called, all returned records will be grouped by title</code>

<p>You can also pass an array of multiple values as well:</p>

	
<code>$o = new Object();<br />
$o->group_by(array('title', 'date'));
<br /><br />// When $o->get() is called, all returned records will be grouped by title and then date</code>
	
<h2>$object->distinct();<br />
</h2>
<p>Adds the &quot;DISTINCT&quot; keyword to  a query</p>

<code>$o = new Object();<br />
$o->distinct();<br />
        <br />// When $o->get() is called, a DISTINCT select of records will be made</code>
		
<h2>$object->having();</h2>
<p>Permits you to write the HAVING portion of your query. There are 2 possible syntaxe, 1 argument or 2:</p>

	
<code>$o = new Object();<br />
$o->having('user_id = 45');
<br />
// When $o->get() is called, all records having a user_id of 45 will be returned<br />
<br />
$o->having('user_id',  45); <br />
// As above, when $o->get() is called, all records having a user_id of 45 will be returned<br />
<br />
</code>
	
<p>You can also pass an array of multiple values as well:</p>


<code>$o = new Object();<br />
$o->having(array('title =' => 'My Title', 'id <' => $id)); <br />
		<br />// When $o->get() is called, all records having a title of My Title and an id less than 45 will be returned</code>
<p>If you are using a database that CodeIgniter escapes queries for, you can prevent escaping content by passing an optional third argument, and setting it to FALSE.</p>
<code>$o = new Object();<br />
$o->having('user_id',  45, FALSE);</code>


<h2>$object->or_having();</h2>
<p>Identical to having(), only separates multiple clauses with &quot;OR&quot;.</p>

<h2>$object->order_by();</h2>
<p>Lets you set an ORDER BY clause. The first parameter contains the name of the column you would like to order by.
The second parameter lets you set the direction of the result.  Options are <kbd>asc</kbd> or <kbd>desc</kbd>, or <kbd>random</kbd>. </p>
	
<code>$o = new Object();<br />
$o->order_by("title", "desc");
<br />
<br />// When $o->get() is called, all returned records will be ordered by title descending</code>

<p>You can also pass your own string in the first parameter:</p>
	
<code>$o = new Object();<br />
$o->order_by('title desc, name asc');
<br />
<br />// When $o->get() is called, all returned records will be ordered by title descending, then name ascending
</code>

<p>Or multiple function calls can be made if you need multiple fields.</p>

<code>$o = new Object();<br />
$o->order_by("title", "desc");<br />
    $o->order_by("name", "asc"); <br />
    <br />// When $o->get() is called, all returned records will be ordered by title descending, then name ascending</code>
<p class="important">Note: random ordering is not currently supported in Oracle or MSSQL drivers. These will default to 'ASC'.</p>

<h2>$object->limit();</h2>
<p>Lets you limit the number of rows you would like returned by the query:</p>

<code>$o = new Object();<br />
$o->limit(10);<br />
<br />
// When $o->get() is called, the number of records returned will be limited to 10</code>


<p>The second parameter lets you set a result offset.</p>

<code>$o = new Object();<br />
$o->limit(10, 20);<br />
<br />
// When $o->get() is called, the number of records returned will be limited to 10, starting from record 20</code>

<p>&nbsp;</p>

<h1><a name="chaining">&nbsp;</a>Method Chaining</h1>

<p>Method chaining allows you to simplify your syntax by connecting multiple functions.  Consider this example:</p>

<code>$o = new Object();<br />
<dfn>$o</dfn><kbd>-></kbd><var>where</var>('id', $id)<kbd>-></kbd><var>limit</var>(10, 20)<kbd>-></kbd><var>get</var>();</code>

<p class="important"><strong>Note:</strong> Method chaining only works with PHP 5.  Funnily enough, DataMapper also only works with PHP 5 so if it works on your server, so will method chaining!</p>

<p>The alternate of the above without method chaining would be:</p>
<code>$o = new Object();<br />
<dfn>$o</dfn><kbd>-></kbd><var>where</var>('id', $id);<br />
<dfn>$o</dfn><kbd>-></kbd><var>limit</var>(10, 20);<br />
<dfn>$o</dfn><kbd>-></kbd><var>get</var>();</code>

<p>&nbsp;</p>

<h1><a name="caching">&nbsp;</a>Active Record Caching</h1>
<p>Since DataMapper uses Active Record for all its queries, it makes sense you should be able to access the Active Record caching methods. While not &quot;true&quot; caching, Active Record enables you to save (or &quot;cache&quot;) certain parts of your queries for reuse later. Normally, when an Active Record call is completed, all stored information is reset for the next call. With caching, you can prevent this reset, and reuse information easily.</p>

<p>Cached calls are cumulative.  If you make 2 cached select() calls, and then 2 uncached select() calls, this will result in 4 select() calls. There are three Caching functions available:</p>
<h2>$object->start_cache()</h2>
<p>This function must be called to begin caching. All Active Record queries of the correct type (see below for supported queries) are stored for later use.</p>
<h2>$object->stop_cache()</h2>
<p>This function can be called to stop caching.</p>
<h2>$object->flush_cache()</h2>
<p>This function deletes all items from the Active Record cache.</p>
<p>Here's a usage example:</p>
<p><code>$o = new Object();<br />
$o->start_cache();<br />
$o->select('field1');<br />
$o->stop_cache();<br />
$o->get();<br />
<br />
// The $o object is populated with all records from its corresponding table, but with only the 'field1' field being populated<br />
<br />
$o->select('field2');<br />
$o->get();<br />
<br />
// The $o object is populated with all records from its corresponding table, but with both the 'field1' and 'field2' fields being populated<br />
<br />
$o->flush_cache();<br />
<br />
$o->select('field2');<br />
$o->get();<br />
<br />
// The $o object is populated with all records from its corresponding table, but with only the 'field2' field being populated<br /></code></p>

<p class="important"> <strong>Note:</strong> The following fields can be cached: ‘select’, ‘from’, ‘join’, ‘where’, ‘like’, ‘group_by’, ‘having’, ‘order_by’, ‘set’</p>

<p>&nbsp;</p>

<h1><a name="advanced">&nbsp;</a>Get (Advanced)</h1>

<p>DataMapper has extended versions of most of its query clauses that allow for advanced querying on relationships.</p>

<p>Let's go through an example to see the benefits.  Let's say we have a User model and a Group model.  A group can have many users but a user can only have one group.  Here's how you would look up all users belonging to the Moderator group without the advanced query:</p>

<code>
// Create user object<br />
$u = new User();<br />
<br />
// Get all users<br />
$u->get();<br />
<br />
// Loop through all users<br />
foreach ($u->all as $user)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;// Get the current user's group<br />
&nbsp;&nbsp;&nbsp;&nbsp;$user->group->get();<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;// Check if user is related to the Moderator group<br />
&nbsp;&nbsp;&nbsp;&nbsp;if ($user->group->name == 'Moderator')<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// ...<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
</code>

<p>Here's how you would do the above, but using an advanced query:</p>

<code>
// Create user object<br />
$u = new User();<br />
<br />
// Get users that are related to the Moderator group<br />
$u->where_related_group('name', 'Moderator')->get();<br />
<br />
// ...
</code>


<p>As you can see, it's a big time saver but not just in the amount of code you write, but also in the number of database queries and overall processing time.</p>


<h2>Supported Query Clauses</h2>

<p>The following are the normal query clauses that you can use in the advanced queries:</p>

<ul>
	<li><dfn>where</dfn></li>
	<li><dfn>or_where</dfn></li>
	<li><dfn>where_in</dfn></li>
	<li><dfn>or_where_in</dfn></li>
	<li><dfn>where_not_in</dfn></li>
	<li><dfn>or_where_not_in</dfn></li>
	<li><dfn>like</dfn></li>
	<li><dfn>not_like</dfn></li>
	<li><dfn>or_like</dfn></li>
	<li><dfn>or_not_like</dfn></li>
	<li><dfn>having</dfn></li>
	<li><dfn>or_having</dfn></li>
</ul>


<h2>$object->{query}_related_{model}($field, $value);</h2>

<p>There are a number of ways you can use these advanced queries, and this is the first usage format.  All examples are done with the User and Group objects scenario.</p>

<ul>
	<li><var>{query}</var> - Replace with supported query type.</li>
	<li><samp>{model}</samp> - Replace with related model name.</li>
	<li><kbd>$field</kbd> - First parameter for chosen query type.</li>
	<li><kbd>$value</kbd> - Second parameter for chosen query type.</li>
</ul>

<p>Here's an example using the <var>where</var> query:</p>

<code>
// Create user<br />
$u = new User();<br />
<br />
// Get all users relating to the Moderator group (goes by 'group', 'name', 'Moderator')<br />
$u-><var>where</var>_related_<samp>group</samp>('<kbd>name</kbd>', '<kbd>Moderator</kbd>')->get();
</code>


<h2>$object->{query}_related($model, $field, $value);</h2>

<p>Alternatively, rather than specifying the related model as part of the method, you could instead supply it as the first parameter.</p>

<ul>
	<li><var>{query}</var> - Replace with supported query type.</li>
	<li><samp>$model</samp> - Supply related model name.</li>
	<li><kbd>$field</kbd> - First parameter for chosen query type.</li>
	<li><kbd>$value</kbd> - Second parameter for chosen query type.</li>
</ul>

<p>Here's an example using the <var>where</var> query:</p>

<code>
// Create user<br />
$u = new User();<br />
<br />
// Get all users relating to the Moderator group (goes by 'group', 'name', 'Moderator')<br />
$u-><var>where</var>_related('<samp>group</samp>', '<kbd>name</kbd>', '<kbd>Moderator</kbd>')->get();
</code>


<h2>$object->{query}_related($related_object, $field, $value);</h2>

<ul>
	<li><var>{query}</var> - Replace with supported query type.</li>
	<li><kbd>$related_object</kbd> - Supply related object.</li>
	<li><strong>Optional:</strong> <kbd>$field</kbd> - First parameter for chosen query type.</li>
	<li><strong>Optional:</strong> <kbd>$value</kbd> - Second parameter for chosen query type.</li>
</ul>

<p class="important"><strong>Note:</strong>&nbsp; Both the <kbd>$field</kbd> and <kbd>$value</kbd> parameters are optional if the <kbd>$related_object</kbd> contains a valid <strong>id</strong>.</p>

<p>Here's an example using the <var>where</var> query:</p>

<code>
// Create and get the Moderator group<br />
$g = new Group();<br />
$g->get_by_name('Moderator');<br />
<br />
// Create user<br />
$u = new User();<br />
<br />
// Get all users relating to the Moderator group (goes by 'group', 'id', $g->id)<br />
$u-><var>where</var>_related(<kbd>$g</kbd>)->get();
</code>

<p>Here's a similar way of doing the above, but with an unpopulated related object (no id):</p>

<code>
// Create and get the Moderator group<br />
$g = new Group();<br />
<br />
// Create user<br />
$u = new User();<br />
<br />
// Get all users relating to the Moderator group (goes by 'group', 'name', 'Moderator')<br />
$u-><var>where</var>_related(<kbd>$g</kbd>, '<kbd>name</kbd>', '<kbd>Moderator</kbd>')->get();
</code>


<p>Which of the available usage formats you use will depend on your personal preference, although you should be consistent with your choice.  It also might depend on whether you have a related object already available to use.</p>

<p class="important"><strong>Note:</strong>&nbsp; For Self Referencing Relationships, you can only use the usage format where you pass a related object with an id, for example:<br /><br />$object->{query}_related($related_object);</p> 

</div>
<!-- END CONTENT -->


<div id="footer">
<p>
Previous Topic:&nbsp;&nbsp;<a href="transactions.html">Transactions</a>
&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Next Topic:&nbsp;&nbsp;<a href="save.html">Save</a>
</p>
<p><a href="http://stensi.com">DataMapper</a> &nbsp;&middot;&nbsp; Copyright &#169; 2008 &nbsp;&middot;&nbsp; <a href="http://stensi.com/">Simon Stenhouse</a></p>
<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2008 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
</div>

<script type="text/javascript" src="../js/mootools.js"></script>
<script type="text/javascript" src="../js/menu.js"></script>
<script type="text/javascript">
<!--
	window.addEvent('domready', function() {

		// Create Menu
		var menu = new Menu({
			basepath: '../',
			pagepath: ''
		});

	});	
//-->
</script>
</body>
</html>